package com.assess.web.service.impl;

import com.assess.web.dao.SchemeMapper;
import com.assess.web.model.PhasesExample;
import com.assess.web.model.Scheme;
import com.assess.web.model.SchemeExample;
import com.assess.web.service.SchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchemeServiceImpl implements SchemeService {


    @Autowired
    private SchemeMapper schemeMapper;

    @Override
    public List<Scheme> listScheme(Integer parentId) {
        if (null == parentId) {
            parentId = 0;
        }
        SchemeExample example = new SchemeExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        return schemeMapper.selectByExample(example);
    }

    @Override
    public Scheme getScheme(int schemeId) {
        return schemeMapper.selectByPrimaryKey(schemeId);
    }

    @Override
    public int save(Integer parentId, String name) {
        Scheme scheme = new Scheme();
        scheme.setName(name);
        if (null != parentId) {
            scheme.setParentId(parentId);
        }

        return schemeMapper.insertSelective(scheme);
    }

    @Override
    public void delete(int id) {
        SchemeExample example = new SchemeExample();
        example.or().andParentIdEqualTo(id);
        example.or().andIdEqualTo(id);
        schemeMapper.deleteByExample(example);
    }
}
